package cn.yunyichina.provider.finance.creator.journal;

import java.util.ArrayList;
import java.util.List;

import cn.yunyichina.utils.commons.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.yunyichina.provider.finance.creator.FinanceAccountJournalCreator;
import cn.yunyichina.provider.finance.entity.AccountJournalPo;
import cn.yunyichina.provider.finance.entity.BoneOrderBo;
import cn.yunyichina.provider.framework.dubbo.service.BaseService;
import cn.yunyichina.provider.iface.entity.common.Response;
import cn.yunyichina.utils.commons.IDUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * 押金单流水
 * <p>
 * 最多3条流水：
 * <br>
 * 1. 主治医生收入<br>
 * 2. 专家收入<br>
 * 3. 平台服务费<br>
 * 
 * @author zhou
 *
 */
public class DepositOrderJournal implements FinanceAccountJournalCreator {
	
	 private static final Logger logger = LoggerFactory.getLogger(DepositOrderJournal.class);
	
	private static final String BUSINESS_DESC = "会诊服务费";

	private BoneOrderBo boneOrderBo;

	/**
	 * 押金单流水
	 * 
	 * @param boneOrderBo
	 */
	public DepositOrderJournal(BoneOrderBo boneOrderBo) {
		super();
		this.boneOrderBo = boneOrderBo;
	}

	@Override
	public List<AccountJournalPo> createJournal() throws Exception {
		logger.info("即将生成押金单流水");
		List<AccountJournalPo> list = new ArrayList<>();

		// 主治（有主治）
		String professorDoctorId = boneOrderBo.getProfessorDoctorId();
		if (StringUtils.isNotBlank(professorDoctorId)) {
			logger.info("该押金单有主治医生");
			AccountJournalPo professorAccountJournalPo = new AccountJournalPo();
			professorAccountJournalPo.setId(IDUtils.getUUID32());
			professorAccountJournalPo.setOrderNo(boneOrderBo.getOrderNo());
			professorAccountJournalPo.setRelationNo(boneOrderBo.getRelationNo());
			professorAccountJournalPo.setOrderType(boneOrderBo.getOrderType());

			// 查询主治医生的信息（机构，科室）
			Response response = BaseService.callHumpFromInternal(
					"yyt.base.orguser.relation.get", "{\"user_id\":\""
							+ professorDoctorId + "\"}");
			String result = response.getResult();
			if (StringUtils.isBlank(result)) {
				logger.error("生成押金单（有主治）流水异常，未查询到主治医生信息 userId: " + professorDoctorId);
				throw new Exception("生成押金单（有主治）流水异常，未查询到主治医生信息 userId: " + professorDoctorId);
			}

			logger.info("主治医生信息: " + result);
			JSONObject resultMap = JSON.parseObject(result);
			professorAccountJournalPo.setOrgId(resultMap.getString("orgId"));
			professorAccountJournalPo.setOrgCode(resultMap.getString("orgCode"));
			professorAccountJournalPo.setOrgName(resultMap.getString("orgName"));
			professorAccountJournalPo.setDeptId(resultMap.getString("deptId"));
			professorAccountJournalPo.setDeptCode(resultMap.getString("deptCode"));
			professorAccountJournalPo.setDeptName(resultMap.getString("deptName"));
			professorAccountJournalPo.setUserId(professorDoctorId);
			professorAccountJournalPo.setUserName(resultMap.getString("userName"));
			professorAccountJournalPo.setBusinessDesc(BUSINESS_DESC);
			professorAccountJournalPo.setAmountDirection(Short.valueOf("1"));
			professorAccountJournalPo.setAmount(boneOrderBo.getProfessorDoctorRecvAmount());
			professorAccountJournalPo.setBusinessType((short) 1);
			professorAccountJournalPo.setCreatedTime(DateUtils.getDateTimeStr());
			list.add(professorAccountJournalPo);
		}

		// 专家
		String appointmentDoctorId = boneOrderBo.getAppointmentDoctorId();
		AccountJournalPo appointmentAccountJournalPo = new AccountJournalPo();
		appointmentAccountJournalPo.setId(IDUtils.getUUID32());
		appointmentAccountJournalPo.setOrderNo(boneOrderBo.getOrderNo());
		appointmentAccountJournalPo.setRelationNo(boneOrderBo.getRelationNo());
		appointmentAccountJournalPo.setOrderType(boneOrderBo.getOrderType());

		// 查询专家的信息（机构，科室）
		Response response = BaseService.callHumpFromInternal(
				"yyt.base.orguser.relation.get", "{\"user_id\":\""
						+ appointmentDoctorId + "\"}");
		String result = response.getResult();
		if (StringUtils.isBlank(result)) {
			logger.error("生成押金单（有主治）流水异常，未查询到专家信息 userId: " + appointmentDoctorId);
			throw new Exception("生成押金单（有主治）流水异常，未查询到专家信息 userId: " + appointmentDoctorId);
		}

		logger.info("专家信息: " + result);
		JSONObject resultMap = JSON.parseObject(result);
		appointmentAccountJournalPo.setOrgId(resultMap.getString("orgId"));
		appointmentAccountJournalPo.setOrgCode(resultMap.getString("orgCode"));
		appointmentAccountJournalPo.setOrgName(resultMap.getString("orgName"));
		appointmentAccountJournalPo.setDeptId(resultMap.getString("deptId"));
		appointmentAccountJournalPo.setDeptCode(resultMap.getString("deptCode"));
		appointmentAccountJournalPo.setDeptName(resultMap.getString("deptName"));
		appointmentAccountJournalPo.setUserId(appointmentDoctorId);
		appointmentAccountJournalPo.setUserName(resultMap.getString("userName"));
		appointmentAccountJournalPo.setBusinessDesc(BUSINESS_DESC);
		appointmentAccountJournalPo.setAmountDirection(Short.valueOf("1"));
		appointmentAccountJournalPo.setAmount(boneOrderBo.getAppointmentDoctorRecvAmount());
		appointmentAccountJournalPo.setBusinessType((short) 4);
		appointmentAccountJournalPo.setCreatedTime(DateUtils.getDateTimeStr());
		list.add(appointmentAccountJournalPo);

		// 平台
		AccountJournalPo platformAccountJournalPo = new AccountJournalPo();
		platformAccountJournalPo.setId(IDUtils.getUUID32());
		platformAccountJournalPo.setOrderNo(boneOrderBo.getOrderNo());
		platformAccountJournalPo.setRelationNo(boneOrderBo.getRelationNo());
		platformAccountJournalPo.setOrderType(boneOrderBo.getOrderType());
		platformAccountJournalPo.setOrgId(boneOrderBo.getPlatformId());
		platformAccountJournalPo.setOrgCode(boneOrderBo.getPlatformCode());
		platformAccountJournalPo.setOrgName(boneOrderBo.getPlatformName());
		platformAccountJournalPo.setBusinessDesc("押金单平台服务费");
		platformAccountJournalPo.setAmountDirection(Short.valueOf("1"));
		platformAccountJournalPo.setAmount(boneOrderBo.getPlatformServiceFee());
		platformAccountJournalPo.setBusinessType((short) 12);
		platformAccountJournalPo.setCreatedTime(DateUtils.getDateTimeStr());
		list.add(platformAccountJournalPo);

		return list;
	}

}
